//
//  ScrollView.m
//  美食百科
//
//  Created by dllo on 16/1/29.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "ScrollView.h"
#import "UIImageView+WebCache.h"
#import "CarouselModel.h"
#define KWIDTH self.frame.size.width
#define KHEIGHT self.frame.size.height
@interface ScrollView ()<UIScrollViewDelegate>

//图片数组
@property(nonatomic, retain) NSMutableArray *imageArray;

//当前页下标
@property(nonatomic, assign) NSInteger index;

//主标题
@property(nonatomic, retain) UILabel *topic;

//副标题
@property(nonatomic, retain) UILabel *subTopic;

//当前页第一个imageView
@property(nonatomic, retain) UIImageView *firstImageView;

//第二个imageView
@property(nonatomic, retain) UIImageView *secondImageView;

//第三个imageView
@property(nonatomic, retain) UIImageView *thirdImageView;

//未显示的左侧imageView
@property(nonatomic, retain) UIImageView *leftImageView;

//未显示右侧imageView
@property(nonatomic, retain) UIImageView *rightImageView;

//当前偏移量
@property(nonatomic, assign) CGFloat offset;

//下侧滚动视图
@property(nonatomic, retain) UIView *myView;

@end

@implementation ScrollView
-(void)dealloc
{
    [_scrollView release];
    [_imageArray release];
    [_topic release];
    [_subTopic release];
    [_array release];
    [_firstImageView release];
    [_secondImageView release];
    [_thirdImageView release];
    [_leftImageView release];
    [_rightImageView release];
    [_myView release];
    [super dealloc];
}

-(id)initWithFrame:(CGRect)frame array:(NSArray *)array
{
    self = [super initWithFrame:frame];
    
    if (self) {
        self.array = [NSArray arrayWithArray:array];
        //初始偏移量为零
        self.offset = 0;
        
        //遍历Model数组,获得图片数组
        self.imageArray = [NSMutableArray array];
        for (CarouselModel *carouse in array) {
            [self.imageArray addObject:carouse.image_url];
        }
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(KWIDTH / 3, 0, KWIDTH / 3, KHEIGHT / 3 * 2)];
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.bounces = NO;
        self.scrollView.pagingEnabled = YES;
        
        //将scrollView的clipsTobounds设为NO,scrollView超出self部分会显示出来
        self.scrollView.clipsToBounds = NO;
        
        self.scrollView.delegate = self;
        self.scrollView.contentSize = CGSizeMake(KWIDTH / 3 * self.imageArray.count + 1, KHEIGHT / 3 * 2);
        
        self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, KHEIGHT / 3, KWIDTH, KHEIGHT / 3 * 2)];
        
        [self addSubview:self.myView];
        [self.myView addSubview:self.scrollView];
        
        //创建imageView,加到scrollView上
        for (int i = 0; i < self.imageArray.count; i++) {
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(KWIDTH / 3 * i, 0, KWIDTH / 3, KHEIGHT / 3 * 2)];
            imageView.userInteractionEnabled = YES;
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            [imageView addGestureRecognizer:tap];
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[i]]placeholderImage:[UIImage imageNamed:@"u=1023456699,842778947&fm=21&gp=0"]];
            imageView.tag = 100 + i;
            [self.scrollView addSubview:imageView];
            [imageView release];
        }
        self.secondImageView = [self viewWithTag:100];
        self.thirdImageView = [self viewWithTag:101];
        self.rightImageView = [self viewWithTag:102];
        self.thirdImageView.transform = CGAffineTransformMakeRotation(M_PI / 36.0);
        self.thirdImageView.alpha = 0.5;
        
        //主标题
        self.topic = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, KWIDTH - 10, 40)];
        
        self.subTopic = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, KWIDTH - 10, 30)];
        
        CarouselModel *carouse = self.array[0];
        self.topic.textColor = [UIColor redColor];

        self.topic.text = carouse.title;
        self.subTopic.text = carouse.sub_title;
        self.topic.font = [UIFont systemFontOfSize:23];
        
        //标题颜色
       // self.topic.textColor = [UIColor redColor];
        self.subTopic.textColor = [UIColor lightGrayColor];
        
        [self addSubview:self.topic];
        [self addSubview:self.subTopic];
    }
    return self;
}

#pragma mark scrollView的协议方法,根据scrollView的偏移量,改变transform
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    float offSetX = scrollView.contentOffset.x;
    
    self.firstImageView.transform = CGAffineTransformMakeRotation(((self.offset - offSetX) / KWIDTH * 3.0 - 1.0) * M_PI / 36.0);
    
    self.secondImageView.transform = CGAffineTransformMakeRotation(((self.offset - offSetX) / KWIDTH * 3.0) * M_PI / 36.0);
    
    self.thirdImageView.transform = CGAffineTransformMakeRotation(((self.offset - offSetX) / KWIDTH * 3.0 + 1.0) * M_PI / 36.0);
    
    self.leftImageView.transform = CGAffineTransformMakeRotation(((self.offset - offSetX) / KWIDTH * 3.0 - 2.0) * M_PI / 36.0);
    
    self.rightImageView.transform = CGAffineTransformMakeRotation(((self.offset - offSetX) / KWIDTH * 3.0 + 2.0) * M_PI / 36.0);
}
#pragma 滚动结束时
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    self.offset = scrollView.contentOffset.x;
    //当前scrollView显示图片的下标
    self.index = scrollView.contentOffset.x / ((KWIDTH / 3.0) - 1);
    
    self.secondImageView = [self viewWithTag:self.index + 100];
    self.secondImageView.alpha = 1;
    
    //将左右两侧图片透明化
    self.firstImageView = [self viewWithTag:self.index + 99];
    self.thirdImageView = [self viewWithTag:self.index + 101];
    self.leftImageView = [self viewWithTag:self.index + 98];
    self.rightImageView = [self viewWithTag:self.index + 102];
    
    self.firstImageView.alpha = 0.5;
    self.thirdImageView.alpha = 0.5;
    
    //改变主标题和副标题
    CarouselModel *carouse = self.array[self.index];
    
    self.topic.text = carouse.title;
    self.subTopic.text = carouse.sub_title;
    
    self.firstImageView.transform = CGAffineTransformMakeRotation(- M_PI / 36.0);
    
    self.secondImageView.transform = CGAffineTransformMakeRotation(0);
    
    self.thirdImageView.transform = CGAffineTransformMakeRotation(M_PI / 36.0);
}

-(void)tapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"执行block时刻");
    //执行block的时刻
    self.block(self.index);
}

//重写hitTest事件,当触摸self时, 返回scrollview事件
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *hitView = [super hitTest:point withEvent:event];
    
    if (hitView == self.myView) {
        return self.scrollView;
    }else{
        return hitView;
    }
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
